const appInstance = getApp();
var url = appInstance.globalData.url;
var Value = '';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    text: '取消',
    classifyList: [],
    videoList: [],
    classifyTitle: [],
    show: false,
    inpValue: '',
    hidden: true,
    isshow: false,
    searchResultList: [],
    valueList: [],
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getFenlei();
    this.OnClassifyList();
    // console.log(wx.getStorageSync('to_ken'))
    //判断用户有没有登录
    if (!wx.getStorageSync('to_ken')) {
      return
    } else {
      // console.log('登录成功')
      this.setData({
        isshow: true
      })
      this.HistorySearchList();
    }
   
  },
  //历史搜索列表
  HistorySearchList() {
    var that = this;
    wx.request({
      url: url + '/user/listKeyword',
      data: {
        pp_token: wx.getStorageSync('to_ken')
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      method: 'POST',
      dataType: 'json',
      responseType: 'text',
      success: function(res) {
        console.log(res.data.data)
        that.setData({
          valueList: res.data.data
        })
      },

    })
  },
  //添加历史搜索
  Addhistory() {
    var that = this;
    wx.request({
      url: url + '/user/addKeyword',
      data: {
        pp_token: wx.getStorageSync('to_ken'),
        keyword: wx.getStorageSync('keyword')
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      method: 'POST',
      dataType: 'json',
      responseType: 'text',
      success: function(res) {
        console.log(res)
      }

    })
  },
  //删除历史搜索
  Delhistory() {
    var that = this;
    wx.request({
      url: url + '/user/deleteKeyword',
      data: {
        pp_token: wx.getStorageSync('to_ken'),
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      method: 'POST',
      dataType: 'json',
      responseType: 'text',
      success: function(res) {
        // console.log(res.data.code)
        if (res.data.code == 200) {
          wx.showModal({
            // title: '确认删除搜索历史',
            content: '确认删除搜索历史',
            success(res) {
              if (res.confirm) {
                console.log('用户点击确定')
                that.setData({
                  isshow: false
                })
              } else if (res.cancel) {
                console.log('用户点击取消')
                that.setData({
                  isshow: true
                })
              }
            }
          })
        }
      },
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  //点击删除按钮
  delHistory() {
    this.Delhistory()
  },

  //返回上级页面
  backIndex(e) {
    // console.log(e.currentTarget.dataset.text)
    var text = e.currentTarget.dataset.text;
    if (text === "取消") {
      wx.navigateBack()
    }
    // else if (text === '搜索') {
    //   //跳转到关键字页面
    //   wx.navigateTo({
    //     url: '../search_keyword/search_keyword?keyword=' + Value
    //   })
    // }

  },
  //键盘输入完成时触发事件
  tapName(e) {
    var that = this;
    // console.log(e.detail.value);
    Value = e.detail.value;
    var keyword = wx.setStorageSync('keyword', Value)
    this.setData({
      text: '搜索',
      show: true,
      hidden: false
    })
    if (!Value && Value.length == 0) {
      this.setData({
        text: '搜索',
        show: false,
        hidden: true
      })

    }
    wx.request({
      url: url + '/video/list',
      data: {
        keyword: Value
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      dataType: 'json',
      success(res) {
        console.log(res.data.data)
        that.setData({
          searchResultList: res.data.data
        })
      }

    })
    
    that.Addhistory();
   
    //跳转到关键字页面
    wx.navigateTo({
      url: '../search_keyword/search_keyword?keyword=' + Value
    })
  },
  blurName() {
    this.setData({
      text: '取消',
      show: false
    })
  },

  removeTap() {
    this.setData({
      inpValue: '',
      hidden: true
    })
  },

  onChange(event) {
    var that = this;
    // console.log(event.detail.index)
    var Index = event.detail.index;
    if (Index === 0) {
      return false;
    }
    var Id = this.data.classifyList[Index - 1].id;
    console.log(Id)

    wx.request({
      url: url + '/video/list',
      data: {
        category: Id
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      dataType: 'json',
      success(res) {
        console.log(res.data.data)
        var classifyTitle = res.data.data;
        that.setData({
          classifyTitle: classifyTitle
        })
      }

    })
  },
  //跳转视频详情
  videoDetail(e) {
    // console.log(e)
    var Id = e.currentTarget.id;
    console.log(Id)
    var that = this;
    wx.request({
      url: url + '/video/detail',
      data: {
        video_id: Id
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      dataType: 'json',
      success(res) {
        console.log(res)
        wx.navigateTo({
          url: '../index/classify/videoDetail/videoDetail?id=' + Id
        })
        that.setData({
          inpValue: ''
        })
      }

    })
  },
  //获取热搜数据
  getFenlei() {
    var that = this;
    wx.request({
      url: url + '/video/list',
      data: {
        type: 0
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      dataType: 'json',
      success(res) {
        console.log(res.data.data)
        that.setData({
          videoList: res.data.data,
        })
      }
    })
  },

  //分类列表
  OnClassifyList() {
    var that = this;
    appInstance.request({
      url: '/category/list',
      success(res) {
        console.log(res)
        that.setData({
          classifyList: res.data.data
        }, function() {
          console.log('classifyList', that.data.classifyList)
        })
      }
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.setData({
      inpValue: '',
      hidden:true
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})